package cn.stylefeng.guns.modular.business.frequency.cmlinkman.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.enums.StatusExceptionEnum;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.cmlinkman.entity.CmLinkman;
import cn.stylefeng.guns.modular.business.frequency.cmlinkman.mapper.CmLinkmanMapper;
import cn.stylefeng.guns.modular.business.frequency.cmlinkman.model.param.CmLinkmanParam;
import cn.stylefeng.guns.modular.business.frequency.cmlinkman.service.CmLinkmanService;
import cn.stylefeng.guns.modular.business.frequency.cmlinkman.enums.CmLinkmanExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 通讯录联系人表 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:17
 */
@Service
public class CmLinkmanServiceImpl extends ServiceImpl<CmLinkmanMapper, CmLinkman> implements CmLinkmanService {

    @Override
    public PageResult<CmLinkman> page(CmLinkmanParam cmLinkmanParam) {

        // 构造条件
        LambdaQueryWrapper<CmLinkman> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<CmLinkman> list(CmLinkmanParam cmLinkmanParam) {

        // 构造条件
        LambdaQueryWrapper<CmLinkman> queryWrapper = new LambdaQueryWrapper<>();


        return this.list(queryWrapper);
    }

    @Override
    public void add(CmLinkmanParam cmLinkmanParam) {

        // 将dto转为实体
        CmLinkman cmLinkman = new CmLinkman();
        BeanUtil.copyProperties(cmLinkmanParam, cmLinkman);

        this.save(cmLinkman);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(CmLinkmanParam cmLinkmanParam) {

        // 根据id查询实体
        CmLinkman cmLinkman = this.queryCmLinkman(cmLinkmanParam);

        // 更新实体
        this.updateById(cmLinkman);

    }

    @Override
    public void edit(CmLinkmanParam cmLinkmanParam) {

        // 根据id查询实体
        CmLinkman cmLinkman = this.queryCmLinkman(cmLinkmanParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(cmLinkmanParam, cmLinkman);

        // 不能修改状态，用修改状态接口修改状态
        cmLinkman.setStatus(null);

        this.updateById(cmLinkman);
    }

    @Override
    public CmLinkman detail(CmLinkmanParam cmLinkmanParam) {
        return this.queryCmLinkman(cmLinkmanParam);
    }

    @Override
    public void changeStatus(CmLinkmanParam cmLinkmanParam) {
        Long id = cmLinkmanParam.getId();
        Integer status = cmLinkmanParam.getStatus();

        // 更新枚举，更新只能更新未删除状态的
        LambdaUpdateWrapper<CmLinkman> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
            .eq(CmLinkman::getId, id)
            .set(CmLinkman::getStatus, status);
        boolean update = this.update(updateWrapper);
        if (!update) {
            throw new ServiceException(StatusExceptionEnum.UPDATE_STATUS_ERROR);
        }
    }


    /**
     * 获取通讯录联系人表
     *
     * @author yujie
     * @since 2023/05/09 17:17
     */
    private CmLinkman queryCmLinkman(CmLinkmanParam cmLinkmanParam) {
        CmLinkman cmLinkman = this.getById(cmLinkmanParam.getId());
        if (ObjectUtil.isEmpty(cmLinkman)) {
            throw new ServiceException(CmLinkmanExceptionEnum.NOT_EXISTED);
        }
        return cmLinkman;
    }
}